package com.ruoyi.quality.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.quality.domain.bo.QualityInternalLossesBo;
import com.ruoyi.quality.domain.vo.QualityInternalLossesVo;
import com.ruoyi.quality.domain.QualityInternalLosses;
import com.ruoyi.quality.mapper.QualityInternalLossesMapper;
import com.ruoyi.quality.service.IQualityInternalLossesService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 质量内部损失Service业务层处理
 *
 * @author ruoyi
 * @date 2023-08-16
 */
@RequiredArgsConstructor
@Service
public class QualityInternalLossesServiceImpl implements IQualityInternalLossesService {

    private final QualityInternalLossesMapper baseMapper;

    /**
     * 查询质量内部损失
     */
    @Override
    public QualityInternalLossesVo queryById(Long lossesId){
        return baseMapper.selectVoById(lossesId);
    }

    /**
     * 查询质量内部损失列表
     */
    @Override
    public TableDataInfo<QualityInternalLossesVo> queryPageList(QualityInternalLossesBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<QualityInternalLosses> lqw = buildQueryWrapper(bo);
        Page<QualityInternalLossesVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询质量内部损失列表
     */
    @Override
    public List<QualityInternalLossesVo> queryList(QualityInternalLossesBo bo) {
        LambdaQueryWrapper<QualityInternalLosses> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<QualityInternalLosses> buildQueryWrapper(QualityInternalLossesBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<QualityInternalLosses> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getLossesTime() != null, QualityInternalLosses::getLossesTime, bo.getLossesTime());
        lqw.eq(StringUtils.isNotBlank(bo.getDeptAbbreviation()), QualityInternalLosses::getDeptAbbreviation, bo.getDeptAbbreviation());
        lqw.eq(StringUtils.isNotBlank(bo.getLossesType()), QualityInternalLosses::getLossesType, bo.getLossesType());
        lqw.eq(StringUtils.isNotBlank(bo.getPartNumber()), QualityInternalLosses::getPartNumber, bo.getPartNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getDefectClassification()), QualityInternalLosses::getDefectClassification, bo.getDefectClassification());
        lqw.eq(bo.getWorkingHours() != null, QualityInternalLosses::getWorkingHours, bo.getWorkingHours());
        lqw.eq(StringUtils.isNotBlank(bo.getLossesPicture()), QualityInternalLosses::getLossesPicture, bo.getLossesPicture());
        lqw.eq(StringUtils.isNotBlank(bo.getReserve1()), QualityInternalLosses::getReserve1, bo.getReserve1());
        lqw.eq(StringUtils.isNotBlank(bo.getReserve2()), QualityInternalLosses::getReserve2, bo.getReserve2());
        return lqw;
    }

    /**
     * 新增质量内部损失
     */
    @Override
    public Boolean insertByBo(QualityInternalLossesBo bo) {
        QualityInternalLosses add = BeanUtil.toBean(bo, QualityInternalLosses.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setLossesId(add.getLossesId());
        }
        return flag;
    }

    /**
     * 修改质量内部损失
     */
    @Override
    public Boolean updateByBo(QualityInternalLossesBo bo) {
        QualityInternalLosses update = BeanUtil.toBean(bo, QualityInternalLosses.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(QualityInternalLosses entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除质量内部损失
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
